<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">

    <link rel="stylesheet" href="../bootstrap-3.3.4/dist/css/bootstrap.min.css"/>
    <link rel="stylesheet" href="../Flat-UI-master/dist/css/flat-ui.min.css"/>
    <script src="../Flat-UI-master/dist/js/vendor/jquery.min.js"></script>
    <script src="../bootstrap-3.3.4/dist/js/bootstrap.min.js"></script>
    <script src="../Flat-UI-master/dist/js/flat-ui.min.js"></script>
    <title></title>
    <style>
        .row{
            margin-left: 20px;
            margin-right: 20px;;
        }
    </style>
    <script src="../js/jquery-3.2.1.min.js"></script>
</head>
<body>
<!-- Static navbar -->
<div class="navbar navbar-default navbar-static-top" role="navigation">
    <div class="container">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                <span class="sr-only">这什么东西</span>
            </button>
            <a class="navbar-brand" href="Index.html">图书商城</a>
        </div>
        <div class="navbar-collapse collapse">
            <ul class="nav navbar-nav">
                <li class="active"><a href="Index.html">首页</a></li>
                <li><a href="Order.html">我的订单</a></li>
                <li><a href="UserInfo.html">个人中心</a></li>
                <li><a href="FriendLink.html">友情链接</a></li>
            </ul>
            <ul class="nav navbar-nav navbar-right hidden-sm">
                <li><a href="Login.html">登录</a></li>
                <li><a href="Register.html">注册</a></li>
                <li>
                    <a href="Cart.html"><span class="glyphicon glyphicon-shopping-cart">购物车</span></a></li>
            </ul>
        </div><!--/.nav-collapse -->
    </div>
</div>
<!--content-->
<div class="container">
    <div class="row thumbnail">
        <div class="col-sm-12">
             <h1 class="text-center" style="margin-bottom: 30px">用户注册</h1>
        </div>
        <div class="col-sm-6">
            <div class="form-horizontal caption">
                <div class="form-group">
                    <label for="username" class="col-sm-3 control-label">用户名</label>
                    <div class="col-sm-8">
                        <input type="text" class="form-control" id="username" placeholder="用户名">
                        <span id="userspan"></span>
                    </div>
                </div>
                <div class="form-group">
                    <label for="password" class="col-sm-3 control-label">密码</label>
                    <div class="col-sm-8">
                        <input type="password" class="form-control" id="password" placeholder="密码">
                        <span id="passspan"></span>
                    </div>
                </div>
                <div class="form-group">
                    <label for="password" class="col-sm-3 control-label">确认密码</label>
                    <div class="col-sm-8">
                        <input type="password" class="form-control" id="password2" placeholder="确认密码">
                        <span id="repwdspan"></span>
                    </div>
                </div>
                <div class="form-group">
                    <label for="inlineRadio1" class="col-sm-3 control-label">性别</label>
                    <div class="col-sm-8">
                        <label class="radio-inline">
                            <input type="radio" name="inlineRadioOptions" id="inlineRadio1" value="男">男
                        </label>
                        <label class="radio-inline">
                            <input type="radio" name="inlineRadioOptions" id="inlineRadio2" value="女">女
                        </label>
                        <span id="genderspan"></span>
                    </div>

                </div>
                <div class="form-group">
                    <label for="telephone" class="col-sm-3 control-label">电话</label>
                    <div class="col-sm-8">
                        <input type="tel" class="form-control" id="telephone" placeholder="电话号码">
                        <span id="telspan"></span>
                    </div>
                </div>
                <div class="form-group">
                    <label for="address" class="col-sm-3 control-label">地址</label>
                    <div class="col-sm-8">
                        <input type="text" class="form-control" id="address" placeholder="地址">
                        <span id="addspan"></span>
                    </div>
                </div>
                <div class="form-group">
                    <label for="email" class="col-sm-3 control-label">邮箱</label>
                    <div class="col-sm-8">
                        <input type="email" class="form-control" id="email" placeholder="邮箱">
                        <span id="emailspan"></span>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-offset-3 col-sm-5">
                        <button type="button" class="btn btn-success btn-block" id="register">注册</button>
                        <span id="span"></span>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-sm-6">
            <div class="caption">
                <h3>免责声明</h3>
                <p>Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec id elit non mi porta gravida at eget metus. Nullam id dolor id nibh ultricies vehicula ut id elit.</p>

            </div>

        </div>

    </div>
</div>


<!--footer-->
<div class="navbar navbar-default navbar-static-bottom">
    版权声明区
</div>
<script>
    var userbool = false;
    var passbool = false;
    var pass2bool = false;
    var emailbool = false;
    var addbool = false;
    var phonebool = false;
    // var gender = $("input[name='inlineRadioOptions']").filter(":checked");

    var genderbool = false;
    $("#username").click(function () {
        $("#userspan").empty();
    });
    $("#username").blur(function () {
        var username = $("#username").val();
        var user = /[A-Za-z0-9_\-\u4e00-\u9fa5]{4,16}/;
        console.log($("#username").val());
        console.log(userbool);
        console.log(user.test(username));
        if (user.test(username)) {
            userbool = true;
            $("#userspan").html("符合要求");
        }else {
            userbool = false;
            $("#userspan").html("用户名必须在4-16位范围内");
        }
    });
    $("#password").click(function () {
        $("#passspan").empty();
    });
    $("#password").blur(function () {
        var password = $("#password").val();
        var pass = /[A-Za-z0-9_\-\u4e00-\u9fa5]{4,16}/;
        if (pass.test(password)) {
            passbool = true;
            $("#passspan").html("符合要求");
        }else {
            userbool = false;
            $("#passspan").html("密码必须在4-16位范围内");
        }
    });
    $("#password2").click(function () {
        $("#repwdspan").empty();
    });
    $("#password2").blur(function () {
        var repwd = $("#password2").val();
        var password = $("#password").val();
        if (repwd == password) {
            pass2bool=true;
            $("#repwdspan").html("符合要求");
        }else {
            pass2bool = false;
            $("#repwdspan").html("确认密码必须和密码一致");
        }
    });
    $("#email").click(function () {
        $("#emailspan").empty();
    });
    $("#email").blur(function () {
        var email = $("#email").val();
        var emailz =  /\w[-\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\.)+[A-Za-z]{2,14}/;
        if (emailz.test(email)) {
            emailbool = true;
            $("#emailspan").html("符合要求");
        }else {
            emailbool = false;
            $("#emailspan").html("不符要求")
        }
    });
    $("#address").click(function () {
        $("#addspan").empty();
    });
    $("#address").blur(function () {
        var address = $("#address").val();
        if (address != null) {
            addbool = true;
            $("#addspan").html("符合要求");
        }else {
            addbool = false;
            $("#addspan").html("地址不能为空");
        }
    });
    $("#telephone").click(function () {
        $("#telspan").empty();
    });
    $("#telephone").blur(function () {
        var phone = $("#telephone").val();
        var tel = /0?(13|14|15|17|18|19)[0-9]{9}/;
        if (tel.test(phone)) {
            phonebool = true;
            $("#telspan").html("符合要求");
        }else {
            phonebool = false;
            $("#telspan").html("不符合要求");
        }
    });
    $("input:radio").click(function () {
        $("#genderspan").empty();
    });
    $("input:radio").blur(function () {
        var gender = $('input:radio:checked').val();
        if (gender != null) {
            genderbool = true;
            $("#genderspan").html("符合要求");
        }else {
            genderbool = false;
            $("#genderspan").html("必须选择性别");
        }
    });
    $("#register").click(function () {
        console.log(userbool);
        console.log(passbool);
        console.log(pass2bool);
        console.log(phonebool);
        console.log(emailbool);
        console.log(addbool);
        console.log(genderbool);
        var username = $("#username").val();
        var password = $("#password").val();
        var email = $("#email").val();
        var address = $("#address").val();
        var phone = $("#telephone").val();
        var gender = $('input:radio:checked').val();
        if (userbool && passbool && pass2bool && phonebool && emailbool && addbool && genderbool) {
            $.ajax({
                url:"/register",
                type:"get",
                data:{
                    username:username,
                    password:password,
                    email:email,
                    address:address,
                    phone:phone,
                    gender:gender
                },
                success:function (re) {
                    if (re.code == 1000){
                        location.href="/Home/Login.html"
                    } else if (re.code == 1001) {
                        // alert("创建失败");
                        console.log("创建失败")
                        $("#span").html("创建失败")
                    }else if (re.code == 1002) {
                        // alert("用户名已存在");
                        console.log("用户名已存在")
                        $("#span").html("用户名已存在")
                    }
                }
            })
        }else {
            // alert("有文本不符合要求");
            console.log("有文本不符合要求")
            $("#span").html("有文本不符合要求")
        }

    })
</script>
</body>
</html>